package fr.mywiki.model.ejbAdapter.services;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;

import fr.mywiki.business.account.AccountLite;
import fr.mywiki.business.entry.EntryLite;
import fr.mywiki.business.structure.StructureLite;
import fr.mywiki.business.versioning.VersionLite;
import fr.mywiki.model.ejbAdapter.EntityHomeCache;
import fr.mywiki.model.ejbAdapter.ServicesAdapter;
import fr.mywiki.model.ejbAdapter.entity.AccountLocal;
import fr.mywiki.model.ejbAdapter.entity.EntryLocal;
import fr.mywiki.model.ejbAdapter.entity.StructureLocal;
import fr.mywiki.model.ejbAdapter.entity.VersionLocal;
import fr.mywiki.model.ejbAdapter.services.business.RemoveLinkServiceBusiness;

/**
 * This session bean deals with adding links to bean objects.
 * 
 * @ejb.bean name = "RemoveLinkService" 
 * 		display-name = "RemoveLinkService"
 * 		description = "RemoveLink Administration Service"
 * 		jndi-name = "ejb/MyWiki/RemoveLinkService"
 * 		remote-business-interface = "fr.mywiki.model.ejbAdapter.services.business.RemoveLinkServiceBusiness"
 * 
 * @ejb.home
 * 		extends = "javax.ejb.EJBHome"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 *  
 * @ejb.interface
 * 		extends = "javax.ejb.EJBObject"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 * 
 * @author tlombard
 */
public class RemoveLinkServiceBean extends ServicesAdapter implements RemoveLinkServiceBusiness,SessionBean {
	/**
	 * The serial version UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Default create method
	 * @throws CreateException
	 * @ejb.create-method 
	 */
	public void ejbCreate() throws CreateException {
	}
	
	/**
	 * Removes the link between the given <tt>account</tt> <code>Value</code> and another
	 * <code>value</code> object, the relation being identified by the parameter <tt>relName</tt>.
	 * 
	 * @param accountId a <code>Long</code> object, being the identifier of the account.
	 * @param removedId a <code>Long</code> object, identifier of the object to be removed.
	 * @param relName the relation to consider
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void removeLinkAccount(Long accountId, Long removedId, String relName)
			throws FinderException {
		AccountLocal account = EntityHomeCache.getInstance().getAccountHome()
				.findByPrimaryKey(accountId);
		if (AccountLite.LINK_ACCOUNT_STRUCTURE.equals(relName)) {
			StructureLocal structure = EntityHomeCache.getInstance()
					.getStructureHome().findByPrimaryKey(removedId);
			if (account.getStructure().equals(structure))
				account.setStructure(null);
		}
	}
	
	/**
	 * Removes the link between the given <tt>entry</tt> <code>Value</code> and another
	 * <code>value</code> object, the relation being identified by the parameter <tt>relName</tt>.
	 * 
	 * @param entryId a <code>Long</code> object, being the identifier of the entry.
	 * @param removedId a <code>Long</code> object, identifier of the object to be removed.
	 * @param relName the relation to consider
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void removeLinkEntry(Long entryId, Long removedId, String relName)
      throws FinderException {
    EntryLocal entry = EntityHomeCache.getInstance().getEntryHome()
        .findByPrimaryKey(entryId);
    if (EntryLite.LINK_VERSION_ENTRY.equals(relName)) {
      entry.getVersions().remove(
          EntityHomeCache.getInstance().getVersionHome().findByPrimaryKey(
              removedId));
    }
  }
	
	/**
	 * Removes the link between the given <tt>structure</tt> <code>Value</code> and another
	 * <code>value</code> object, the relation being identified by the parameter <tt>relName</tt>.
	 * 
	 * @param structureId a <code>Long</code> object, being the identifier of the structure.
	 * @param removedId a <code>Long</code> object, identifier of the object to be removed.
	 * @param relName the relation to consider
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void removeLinkStructure(Long structureId, Long removedId,
			String relName) throws FinderException {
		StructureLocal structure = EntityHomeCache.getInstance().getStructureHome()
				.findByPrimaryKey(structureId);
		if (StructureLite.LINK_PARENT_TO_CHILDREN.equals(relName)) {
			structure.getChildren().remove(
					EntityHomeCache.getInstance().getStructureHome().findByPrimaryKey(
							removedId));
		} else if (StructureLite.LINK_CHILD_TO_PARENT.equals(relName)) {
			StructureLocal struct = EntityHomeCache.getInstance().getStructureHome()
					.findByPrimaryKey(removedId);
			StructureLocal parent = structure.getParent();
			if (parent != null && parent.equals(struct))
				structure.setParent(null);
		} else if (StructureLite.LINK_ACCOUNT_STRUCTURE.equals(relName)) {
			structure.getAccounts().remove(
					EntityHomeCache.getInstance().getAccountHome().findByPrimaryKey(
							removedId));
		}
	}
	
	/**
	 * Removes the link between the given <tt>version</tt> <code>Value</code>
	 * and another <code>value</code> object, the relation being identified by
	 * the parameter <tt>relName</tt>.
	 * 
	 * @param versionId
	 *          a <code>Long</code> object, being the identifier of the version.
	 * @param removedId
	 *          a <code>Long</code> object.
	 * @param relName
	 *          the relation to consider
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void removeLinkVersion(Long versionId, Long removedId, String relName)
			throws FinderException {
		VersionLocal version = EntityHomeCache.getInstance().getVersionHome()
				.findByPrimaryKey(versionId);
		if (VersionLite.LINK_VERSION_ENTRY.equals(relName)) {
			EntryLocal entry = EntityHomeCache.getInstance()
					.getEntryHome().findByPrimaryKey(removedId);
			if (version.getEntry().equals(entry))
				version.setEntry(null);
		}
	}
}